﻿//页面必须引用Jquery库
var appid = "wxd51f631bbd5d9e74"; //公众号APPID
var BaseUrl = "http://open.1juke.cn"; //授权服务器地址
var isDebug = false; //是否调试模式（调试模式支持PC访问，会返回虚拟用户信息。在微信浏览器下此参数无效）
var WxUserInfo = null; //授权后用户信息
var Init = true;

$(document).ready(function() {
    if (!localStorage.getItem("WxUserInfo" + appid)) { //判断用户是否已经授权
        if (!localStorage.getItem("uuid")) {
            localStorage.setItem("uuid", uuid(12, 16)); //生成用户标识
        }
        $.ajax({
            url: BaseUrl + "/OAuth/GetUserInfo",
            dataType: "json",
            async: true,
            data: { appid: appid, uuid: localStorage.getItem("uuid"), url: location.href.split('#')[0], isDebug: isDebug },
            type: "POST",
            success: function(r) {
                if (r.IsSuccess) { //授权成功获取用户信息
                    WxUserInfo = r.Data;
                    if (globalInit) {
                        globalInit();
                    }
                    localStorage.setItem("WxUserInfo" + appid, JSON.stringify(r.Data));
                    getJsSDK();
                } else {
                    if (r.Data) {
                        location.href = r.Data;
                    } else {
                        console.log(r.Message);
                        //window.location.reload();
                    }
                }
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log("err:" + jqXHR.statusText);
                //window.location.reload();
            }
        });
    } else {
        WxUserInfo = JSON.parse(localStorage.getItem("WxUserInfo" + appid));
        if (globalInit) {
            globalInit();
        }
        getJsSDK();
    }
})

function getJsSDK() { //获取JSSDK信息
    if (isWX) {
        $.ajax({
            url: BaseUrl + "/OAuth/GetJsSDK",
            dataType: "json",
            async: true,
            data: { appid: appid, url: location.href.split('#')[0] },
            type: "POST",
            success: function(r) {
                wx.config({
                    debug: false, // 调试模式
                    appId: r.AppId,
                    timestamp: r.Timestamp,
                    nonceStr: r.NonceStr,
                    signature: r.Signature,
                    jsApiList: api
                });
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log("err:" + jqXHR.statusText);
            }
        });
    }
    //  else {
    //      window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxd51f631b&redirect_uri=%3fauth%3&response_type=code&scope=snsapi_base&state=gh_db&connect_redirect=1#wechat_redirect";
    //  }
}
var isWX = function() { //判断是否微信浏览器
    var ua = navigator.userAgent.toLowerCase();
    return (/micromessenger/.test(ua)) ? true : false;
}();

function uuid(len, radix) {
    var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
    var uuid = [],
        i;
    radix = radix || chars.length;
    if (len) {
        for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
    } else {
        var r;
        uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
        uuid[14] = '4';
        for (i = 0; i < 36; i++) {
            if (!uuid[i]) {
                r = 0 | Math.random() * 16;
                uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
            }
        }
    }
    return uuid.join('');
}
var api = ["checkJsApi", "onMenuShareTimeline", "onMenuShareAppMessage", "onMenuShareQQ", "onMenuShareWeibo", "hideMenuItems", "showMenuItems", "hideAllNonBaseMenuItem", "showAllNonBaseMenuItem", "translateVoice", "startRecord", "stopRecord", "onRecordEnd", "playVoice", "pauseVoice", "stopVoice", "uploadVoice", "downloadVoice", "chooseImage", "previewImage", "uploadImage", "downloadImage", "getNetworkType", "openLocation", "getLocation", "hideOptionMenu", "showOptionMenu", "closeWindow", "scanQRCode", "chooseWXPay", "openProductSpecificView", "addCard", "chooseCard", "openCard"];



function udata(str1, str2, str3, str4, str5, str6, str7) {
    var datetime = getNowFormatDate()
    $.ajax({
        type: "Post",
        url: "http://udata.1juke.cn/dal.aspx",
        data: {
            "ImportID": str1,
            "手机号码": str2,
            "区域": str3,
            "公司": str4,
            "活动名称": str5,
            "项目": str6,
            "姓名": str7,
            "参与时间": datetime,
            "OpenID": WxUserInfo.openid,
            "RandomCode": Math.random() * 10000
        },
        async: false,
        cache: false,
        dataType: "json",
        success: function(result) {
            console.log(result.Msg)
        }
    })
}


function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate +
        " " + date.getHours() + seperator2 + date.getMinutes() +
        seperator2 + date.getSeconds();
    return currentdate;
}